//
//  IOPackSerialController.java
//  SketchServer
//
//  Created by Brian Hinch on 10/18/08, sitting in Arlanda Airport, Sweden.
//  Copyright 2008 Tellart. All rights reserved.
//

import javax.swing.*;
import javax.swing.border.*;
import java.util.*;
import java.io.*;
import java.nio.channels.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import java.util.EventObject;
import java.util.logging.*;
import java.util.regex.*;
import javax.swing.table.*;

/***********************************************************************************************
 * SketchFileFilter is a FileFilter that accepts files which cause isProject to return true, and which
 * are *not* present in the List passed to the filter at construction. It works with the File class
 * and provides a static method to get a filter for use with a JFileChooser.
 *
 */
class SketchFileFilter implements java.io.FileFilter {
	
	public final static String DASHCODE_PROJECT_FILE_EXTENSION = ".dcproj";
    public final static String PROJECT_INDEX_FILE_NAME = "index.html";
	
	/**
	 * constructs a new SketchFileFilter
	 *
	 */
	public SketchFileFilter() {
		super();
	} //SketchFileFilter
	
	public static boolean isDashcodeProject(File candidate) {
		System.out.println("SketchFileFilter.isDashcodeProject: " + candidate.getPath());
		return candidate.getPath().toLowerCase().endsWith(DASHCODE_PROJECT_FILE_EXTENSION);
	}
    
    public static boolean isProjectIndex(File candidate) {
		System.out.println("SketchFileFilter.isProjectIndex: " + candidate.getPath());
		return candidate.isFile() && candidate.getPath().toLowerCase().endsWith(PROJECT_INDEX_FILE_NAME);
    }
	
	public boolean accept(File candidate) {
		return candidate.isDirectory() || SketchFileFilter.isProjectIndex(candidate) || SketchFileFilter.isDashcodeProject(candidate);
	} //accept
	
	
	/**
	 * returns a FileFilter suitable for use with a JFileChooser component which is backed by a SketchFileFilter
	 *
	 */
	static javax.swing.filechooser.FileFilter getSwingFilter() {
		return new javax.swing.filechooser.FileFilter() {
			SketchFileFilter ff = new SketchFileFilter();
			
			public boolean accept(File candidate) {
				return ff.accept(candidate);
			} //accept
			
			public String getDescription() {
				return "Sketches";
			}  //getDescription
		};
	} //getSwingFilter
	
} //SketchFileFilter class
